package com.homegoods.classify.persenter;

import com.homegoods.classify.item.ADViewItem;
import com.homegoods.classify.model.Constant;
import com.homegoods.classify.model.GoodsModel;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.Tuple2;

import java.util.List;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @date 2017/5/2
 */

public class SubClassifyPresenter extends AbstractRefreshPresenter<RefreshingView> {

    private ADViewItem adViewItem;
    private String requestType;
    private Random random = new Random();

    public SubClassifyPresenter(RefreshingView view, String type) {
        super(view);
        requestType = type;
        adapter.removeItem(emptyItem);
        adViewItem = new ADViewItem(null);
    }

    public void setType(String type) {
        this.requestType = type;
        refreshData();
    }

    @Override
    protected void addHead() {
        super.addHead();
        adapter.setItem(adViewItem);
        adapter.notifyItemInserted(0);
        getHeadViewData();
    }

    @Override
    protected void fetchData(final boolean loadMore) {
        //todo 此部分只请求热门分类数据
        addSubscription(refreshGoodsModel()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(goodsModels -> parserData(loadMore, goodsModels)));
    }

    /**
     * 在此请求轮播图数据
     */
    private void getHeadViewData() {
        //todo 每次刷新的时候，请求数据包括头部的广告、全场热销、热门分类
        Observable<String> ad = Observable.just(Constant.TEST_AD_URL + random.nextInt());

        Observable<List<GoodsModel>> observable = Observable.range(0, 5)
                .map(integer -> {
                    GoodsModel model = new GoodsModel();
                    model.title = "热门产品" + integer;
                    model.avatar = Constant.TEST_IMG_URL + random.nextInt(100);
                    return model;
                })
                .buffer(5);

        addSubscription(Observable.zip(ad, observable, Tuple2::new)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(r -> {
                    String imgUrl = r.getT0();
                    List<GoodsModel> newProductModels = r.getT1();

                    adViewItem.setImages(imgUrl);
                    adViewItem.setGoodsItems(newProductModels);

                }));
    }

    private Observable<List<GoodsModel>> refreshGoodsModel() {
        return Observable.range(0, 12)
                .map(integer -> {
                    GoodsModel model = new GoodsModel();
                    model.title = "热门产品" + (page * PER_PAGE_COUNT + integer);
                    model.avatar = Constant.TEST_IMG_URL + random.nextInt(10);
                    return model;
                })
                .buffer(12);

    }


}
